﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace ConceptCorporateAdmin
{
    public class DbObject
    {
        public DataRow DR;

        public DbObject()
        {
        }

        public DbObject(DataRow dr)
        {
            loadFromDR(dr, this);
        }

        public void loadFromDR(DataRow dr, object obj)
        {
            this.DR = dr;
            PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(obj);
            for (int i = 0; i < coll.Count; i++)
            {
                //seteaza proprietatile
                if (coll[i].PropertyType.Equals(Type.GetType("System.String")))
                    coll[i].SetValue(obj, dr[coll[i].Name] + "");
                
                if (coll[i].PropertyType.Equals(Type.GetType("System.Int32")))
                    coll[i].SetValue(obj, int.Parse(dr[coll[i].Name] + ""));
                
                if (coll[i].PropertyType.Equals(Type.GetType("System.Boolean")))
                    coll[i].SetValue(obj, bool.Parse(dr[coll[i].Name] + ""));

                if (coll[i].PropertyType.Equals(Type.GetType("System.Double")))
                    coll[i].SetValue(obj, double.Parse(dr[coll[i].Name] + ""));
            }
        }

        public int getId()
        {
            int ret = -1;
            PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(this);
            for (int i = 0; i < coll.Count; i++)
            {
                if (!coll[i].Name.Equals("Id")) continue;
                ret = int.Parse(coll[i].GetValue(this).ToString());
            }
            return ret;
        }

        public virtual void Delete()
        {
            string query = "DELETE from \"" + getTableName() + "\" WHERE \"Id\"= " + this.getId();
            DbConnection.Instance.NonQuery(query);
        }

        public virtual void Update()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE \"" + getTableName() + "\" SET ");
            PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(this);
            for (int i = 0; i < coll.Count; i++)
            {
                //adauga fiecare camp
                sb.Append("\"" + coll[i].Name + "\"='");
                sb.Append(coll[i].GetValue(this) + "',");
            }
            sb.Remove(sb.Length - 1, 1);
            if (getId() != -1)
                sb.Append(" WHERE \"Id\"='" + getId() + "'");
            Console.WriteLine("sql=" + sb.ToString());
            DbConnection.Instance.NonQuery(sb.ToString());
        }

        public int getNextId()
        {
            String sql = "select gen_id(\"GEN_" + getTableName() + "_ID\", 0) from rdb$database;";
            DataTable dt = DbConnection.Instance.Query(sql);
            return int.Parse(dt.Rows[0][0].ToString()) + 1;
        }

        public bool getFromId(int id)
        {
            String sql   = "SELECT * FROM \"" + getTableName() + "\" WHERE \"Id\"='" + id + "';";
            DataTable dt = DbConnection.Instance.Query(sql);
            bool ret     = false;
            if (dt.Rows.Count > 0)
            {
                loadFromDR( dt.Rows[0], this );
                ret      = true;
            }
            return ret;
        }

        public int CreateNew()
        {
            int next_id      = getNextId() ;
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO \"" + getTableName() + "\"( ");
            PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(this);
            foreach (PropertyDescriptor pr in coll)
            {
                if (!pr.Name.Equals("Id")) sb.Append("\"" + pr.Name + "\",");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append(") VALUES (");
            foreach (PropertyDescriptor pr in coll)
            {
                string name = pr.Name;
                string value = "";
                object v = pr.GetValue(this);
                if (v != null) value = v.ToString();
                if (!name.Equals("Id")) sb.Append("'" + value + "',");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append(")");
            DbConnection.Instance.NonQuery(sb.ToString());
            //seteaza idul
            coll["Id"].SetValue(this, next_id);
            return next_id;
        }

        //override this
        protected virtual string getTableName()
        {
            return "";
        }



    }

}
